/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Address} from '../../utils/address.js';
var address = new Address();

Page({
    data: {
        loadingHidden: false,
        addressList: []
    },
    /**
     * 获取收货地址,绑定
     */
    onShow: function() {
        var that = this;
        address.getAddress((res)=> {
            that.setData({
                addressList: res,
                loadingHidden: true
            })
        })
    },
    /**
     * 删除地址
     */
    deleteAddress: function(event) {
        var that = this,
            id = address.getDataSet(event, 'id'),
            index = address.getDataSet(event, 'index');
        wx.showModal({
            title: "提示",
            content: "确认删除收货地址？",
            success: function(e) {
                if (e.confirm) {
                    wx.showLoading({
                        title: "正在删除",
                        mask: 1
                    })
                    address.deleteAddress(id, (flag)=> {
                        if(!flag){
                            that.showTips('操作提示', '地址信息删除失败！')
                        }
                        that.data.addressList.splice(index, 1); //移除
                        that.setData({
                            addressList: that.data.addressList
                        })
                        wx.hideLoading();
                    })
                }
            }
        })
    },
    /**
     * 默认设为
     */
    setDefault: function(event) {
        var that = this,
            id = address.getDataSet(event, 'id'),
            df = address.getDataSet(event, 'df'),
            index = address.getDataSet(event, 'index');
        if (df != 1) {
            wx.showLoading({
                title: "正在设置",
                mask: 1
            })
            address.setDefault(id, (flag)=> {
                if(!flag){
                    that.showTips('操作提示', '设为默认失败！')
                }
                var list = that.data.addressList;
                for (let i in list) {
                    if (list[i].df == 1) {
                        list[i].df = 0; //取消
                    }
                }
                list[index].df = 1; //设置
                that.setData({
                    addressList: list
                })
                wx.hideLoading();
            })
        }
    }
})